$(function(){
	var approve_id;
	var data={};
	data.page=1;
	data.pagenum=10;
	var approvestate=1;
	var approvemain={}
	var where_json={};
		where_json.maintenance_record_id=['in',"{$Think.get.maintenance_record_id}"]
	data.where_json=JSON.stringify(where_json)
	
	
	$('.back').on('click',function(){
		window.history.go(-1)
	})
	ajaxservicedetail(data)
	//请求审批维修记录信息
	
	//请求维修记录信息
	function ajaxservicedetail(data){
		$.ajax({
			type:'post',
			data:data,
			url:"{:url('adminapi/Maintenancerecord/recordList')}",
			success:function(res){
				$("#loading-first").fadeOut(500);
				console.log(res)
				console.log(approvestate)
				for(var key in res.list[0]){
					$('.'+key).html(res.list[0][key])
				}
				
					//审批未添加
					$('.check_main').css('display','none')
					$('.no_check_main').css('display','block')
					
					$('.check').removeAttr('disabled')
					$('.check').html('请求审批')
					$('.checkedit').css('display','none')
				
				$('.addmaterial').on('click',function(){
					$('.material_cost_info_all').append(`
						<li class="am-u-sm-12 margin_bottom_10" style="border: 1px solid #ccc;border-radius: 5px;padding: 10px;">
							<div class="am-u-sm-10 cl-p">
							<div class="am-u-sm-12 cl-p">
								<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="material_name" placeholder="配件名称"/></div>
								<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="material_type" placeholder="配件型号"/></div>
								<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="supplier" placeholder="供应商"/></div>
								<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="tel" placeholder="电话"/></div>
							</div>
							<div class="am-u-sm-12 cl-p">
								<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="unit" placeholder="配件单位"/></div>
								<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="num" placeholder="数量"/></div>
								<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="price" placeholder="单价"/></div>
								<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="total" placeholder="合计"/></div>
							</div>
							</div>
						</li>
					`)
				})
					$('.addworking').on('click',function(){
						$('.working_hours_all').append(`
							<li class="am-u-sm-12 margin_bottom_10" style="border: 1px solid #ccc;border-radius: 5px;padding: 10px;">
								<div class="am-u-sm-10 cl-p">
									<div class="am-u-sm-12 cl-p">
										<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="working_name" placeholder="维修项目"/></div>
										<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="price" placeholder="工时单价"/></div>
										<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="hour" placeholder="工时数"/></div>
										<div class="am-u-sm-3"><input type="text" class="tpl-form-input driver" name="total" placeholder="合计"/></div>
									</div>
								
								</div>
							</li>
						`)
					})
				//审核
				$('.check').on('click',function(){
					Load()
					var datacheck={};
					datacheck.maintenance_record_id={$Think.get.maintenance_record_id};
					datacheck.maintenance_id=res.list[0].maintenance_id;
					datacheck.company_id=$('.company_all_id').val();
					datacheck.maintenance_time=res.list[0].start_date+" 00:00:00";
					datacheck.contact_person=res.list[0].leader;
					datacheck.contact_number=res.list[0].service_phone;
					datacheck.kilometers=res.list[0].km;
					
					datacheck.approve_person=$('input[name="approve_person"]').val();
					datacheck.approve_orgn=$('input[name="approve_orgn"]').val();
					datacheck.approve_desc=$('input[name="approve_desc"]').val();
					datacheck.approve_status=2;
						
						
					var material_cost_info=[];
					var working_hours=[];
					$('.material_cost_info_all li').each(function(){
						var list={}
						$(this).find('input').each(function(){
							list[$(this).attr('name')]=$(this).val()
						})
						console.log(list)
						material_cost_info.push(list)
					})
					datacheck.material_cost_info=JSON.stringify(material_cost_info);
					$('.working_hours_all li').each(function(){
						var list={}
						$(this).find('input').each(function(){
							list[$(this).attr('name')]=$(this).val()
						})
						console.log(list)
						working_hours.push(list)
					})
					datacheck.working_hours=JSON.stringify(working_hours);
					console.log(datacheck)
					$.ajax({
						type:'post',
						url:"{:url('adminapi/Maintenanceapprove/add')}",
						data:datacheck,
						success:function(res){
							console.log(res)
							Load(1)
							if(res.code==1){
								Load(1)
								Msg('审批已提交')
								window.history.go(-1)
							}else{
								Err(res.msg)
							}
						},
						error:function(){
							Load(1)
							Err('请求失败')
						}
					})
				})
				//编辑保存
				$('.keep').on('click',function(){
					Load()
						var datakeep={};
						datakeep.maintenance_record_id={$Think.get.maintenance_record_id};
						datakeep.maintenance_id=res.list[0].maintenance_id;
						datakeep.company_id=$('.company_all_id').val();
						datakeep.maintenance_time=res.list[0].start_date+" 00:00:00";
						datakeep.contact_person=res.list[0].leader;
						datakeep.contact_number=res.list[0].service_phone;
						datakeep.kilometers=res.list[0].km;
						datakeep.approve_id=approve_id
						datakeep.approve_person=$('input[name="approve_person"]').val();
						datakeep.approve_orgn=$('input[name="approve_orgn"]').val();
						datakeep.approve_desc=$('input[name="approve_desc"]').val();
						datakeep.approve_status=2;
						
						
						var material_cost_info=[];
						var working_hours=[];
						$('.material_cost_info_all li').each(function(){
							var list={}
							$(this).find('input').each(function(){
								list[$(this).attr('name')]=$(this).val()
							})
							console.log(list)
							material_cost_info.push(list)
						})
						datakeep.material_cost_info=JSON.stringify(material_cost_info);
						$('.working_hours_all li').each(function(){
							var list={}
							$(this).find('input').each(function(){
								list[$(this).attr('name')]=$(this).val()
							})
							console.log(list)
							working_hours.push(list)
						})
						datakeep.working_hours=JSON.stringify(working_hours);
						$.ajax({
							type:'post',
							url:"{:url('adminapi/Maintenanceapprove/edit')}",
							data:datakeep,
							success:function(res){
								Load(1)
								console.log(res)
								if(res.code==1){
									Msg('审批完成')
									window.history.go(0)
								}else{
									Err(res.msg)
								}
							},
							error:function(){
								Load(1)
								Err('请求失败')
							}
						})
					})
			},
			error:function(){
				$("#loading-first").fadeOut(500);
				Err('请求失败')
			}
		})
	}
	function cost_info_list(main){
		var costtbody="";
		for(var idx=0;idx<main.length;idx++){
			costtbody+=`<tr>`
			for(var key in main[idx]){
				costtbody+=`<td class='${key}'>${main[idx][key]}</td>`
			}
			costtbody+=`</tr>`
		}
		return costtbody
	}
	function work_hour_list(main){
		var worktbody="";
		for(var idx=0;idx<main.length;idx++){
			worktbody+=`<tr>`
			for(var key in main[idx]){
				worktbody+=`<td class='${key}'>${main[idx][key]}</td>`
			}
			worktbody+=`</tr>`
		}
		return worktbody
	}
})
